import api from '../../js/api';
import { formatDate } from "../../js/util"
const dayjs = require('dayjs')

Page({

    data: {
        productList: [],
        totalWeight: 0,
        sub_fld5: null, //入库类型 0：正常入库；1:返库；2：盘盈入库
        sub_fld5_txt: null,
        rdate: null, //到货日期
        gcode: null, //品类
        gcode_txt: null, //品类
        scode: null, //堆场
        scode_txt: null,
        sub_fld4: null, //垛位
        sub_fld3: null, //车牌号
        wcode: null, //入库人
        remark: null, //备注
        state: 0, //垛位状态 0空 1满
        state_txt: "空垛",
        currentDate: new Date().getTime(),
        // 
        listRukuleixing: [{ "name": "正常入库", id: 0 }, { "name": "返库", id: 1 }, { "name": "盘盈入库", id: 2 }],
        listPinlei: [],
        listDuichang: [],
        listZhuangtai: [{ "name": "空垛", id: 0 }, { "name": "满垛", id: 1 }],
        //
        isShowRiqi: false,
        isShowSub_fld5: false,
        isShowGcode: false,
        isShowScode: false,
        isShowState: false
    },



    onShow() {

        //console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'))
        let pl = wx.getStorageSync('pl')
        let totalWeight = 0
        if (pl) {
            this.setData({ "productList": pl })
            totalWeight = pl.reduce((sum, item) => sum + item.weight * 1, 0);
        }

        let user = wx.getStorageSync('user')


        //总共重量

        this.setData({ totalWeight: totalWeight, wcode: user.wcode })
        //获取品类
        api.getProductType().then((r) => {
            let res = r.data.map(({ code, cname }) => ({ code, name: cname }))
            this.setData({ listPinlei: res })
        })

        //获取堆场
        api.getDuichang().then((r) => {

            let res = r.data.map(({ scode, sname }) => ({ scode, name: sname }))
            this.setData({ listDuichang: res })
        })

        //console.log(dayjs().format("YYYY-MM-DD"));
        let rdate =   dayjs().format('YYYY-MM-DD') //wx.getStorageSync('init_rdate') || ""
        let sub_fld5_txt = wx.getStorageSync('init_sub_fld5_txt')
        let sub_fld5 = wx.getStorageSync('init_sub_fld5')

        let gcode_txt = wx.getStorageSync('init_gcode_txt')
        let gcode = wx.getStorageSync('init_gcode')

        let scode_txt = wx.getStorageSync('init_scode_txt')
        let scode = wx.getStorageSync('init_scode')

        this.setData({ rdate, sub_fld5_txt, sub_fld5, gcode_txt, gcode, scode_txt, scode })

    },

    showPop(e) {
        const key = e.currentTarget.id
        let data = {}
        data[key] = true
        this.setData(data)
    },
    onClose() {
        this.setData({
            isShowRiqi: false,
            isShowSub_fld5: false,
            isShowGcode: false,
            isShowScode: false,
            isShowSub_fld4: false
        })
    },
    selectDate(e) {
        let date = new Date(e.detail)
        let rdate = formatDate(date)
        this.setData({
            isShowRiqi: false,
            rdate
        })
        wx.setStorageSync('init_rdate', rdate)
    },
    selectSub_fld5(e) {
        this.setData({
            sub_fld5_txt: e.detail.name,
            sub_fld5: e.detail.id,
            isShowSub_fld5: false
        })

        wx.setStorageSync('init_sub_fld5_txt', e.detail.name)
        wx.setStorageSync('init_sub_fld5', e.detail.id)
    },

    selectGcode(e) {
        this.setData({
            gcode_txt: e.detail.name,
            gcode: e.detail.code,
            isShowGcode: false
        })

        wx.setStorageSync('init_gcode_txt', e.detail.name)
        wx.setStorageSync('init_gcode', e.detail.code)
    },

    selectScode(e) {
        this.setData({
            scode_txt: e.detail.name,
            scode: e.detail.scode,
            isShowScode: false
        })

        wx.setStorageSync('init_scode_txt', e.detail.name)
        wx.setStorageSync('init_scode', e.detail.scode)
    },
    selectState(e) {
        this.setData({
            state_txt: e.detail.name,
            state: e.detail.id,
            isShowState: false
        })
    },

    save() {
        let that = this
        let reqRuku = {}
        reqRuku.sub_fld5 = this.data.sub_fld5
        reqRuku.rdate = this.data.rdate
        reqRuku.gcode = this.data.gcode
        reqRuku.scode = this.data.scode
        reqRuku.sub_fld4 = this.data.sub_fld4
        reqRuku.sub_fld3 = this.data.sub_fld3
        reqRuku.wcode = this.data.wcode
        reqRuku.remark = this.data.remark
        reqRuku.state =  this.data.state+""
        reqRuku.data = this.data.productList

        if (reqRuku.data.length == 0) {
            wx.showToast({ title: '请先扫码', icon: 'error', duration: 1000 })
            return
        }

        if (!reqRuku.rdate) {
            wx.showToast({ title: '请选择到货日期', icon: 'error', duration: 1000 })
            return
        }

        if (reqRuku.sub_fld5 === null) {
            wx.showToast({ title: '请选择入库类型', icon: 'error', duration: 1000 })
            return
        }

        if (reqRuku.gcode === null) {
            wx.showToast({ title: '请选择品类', icon: 'error', duration: 1000 })
            return
        }

        if (reqRuku.scode === null) {
            wx.showToast({ title: '请选择堆场', icon: 'error', duration: 1000 })
            return
        }

        if (!reqRuku.sub_fld4) {
            wx.showToast({ title: '请输入垛位', icon: 'error', duration: 1000 })
            return
        }

        if (reqRuku.state === null) {
            wx.showToast({ title: '请选择垛位状态', icon: 'error', duration: 1000 })
            return
        }

        console.log(reqRuku)
        api.postConfirmIn(reqRuku).then(res => {
            console.log(res);

            if (res.code === "0") {
                wx.showModal({
                    title: '提示',
                    content: '操作成功',
                    showCancel: false,
                    success(rr) {
                        if (rr.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                })
                that.setData({ productList: [], totalWeight: 0 })
                wx.setStorageSync('pl', [])
            } else {
                wx.showModal({
                    title: '提交失败',
                    content: res.msg,
                    cancelText: "删除重复",
                    success(rr) {
                        if (rr.confirm) {} else if (rr.cancel) {
                            let chongfu = res.fldRep
                            if (chongfu) {
                                let chongfuSet = new Set(chongfu.split(","))
                                let newProductList = reqRuku.data.filter(item => {
                                    return !chongfuSet.has(item.skuno)
                                })
                                let totalWeight = newProductList.reduce((sum, item) => sum + item.weight * 1, 0);
                                that.setData({ productList: newProductList, totalWeight: totalWeight })
                                wx.setStorageSync('pl', newProductList)
                            }
                        }
                    }
                })
            }

        })

    }


})